package lv.solcraft.replication.server.rest.restlets;

import lv.solcraft.replication.server.boot.ServerConfig;

import org.restlet.Context;
import org.restlet.data.CharacterSet;
import org.restlet.data.Encoding;
import org.restlet.data.Language;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

import com.noelios.restlet.application.EncodeRepresentation;

public class ClientConfigResource extends Resource {

    private ServerConfig serverConfig;

    @Override
    public void init(Context context, Request request, Response response) {
        super.init(context, request, response);
        getVariants().add(new Variant(MediaType.TEXT_PLAIN));
    }

    @Override
    public boolean allowGet() {
        return true;
    }

    @Override
    public Representation getRepresentation(Variant variant) {
        StringRepresentation stringRepresentation = new StringRepresentation(serverConfig.toClientJSONReloaded().toString(2), MediaType.TEXT_PLAIN, Language.ALL, CharacterSet.UTF_8);
        return new EncodeRepresentation(Encoding.GZIP, stringRepresentation);
    }

    public void setServerConfig(ServerConfig serverConfig) {
        this.serverConfig = serverConfig;
    }
}
